package il.ac.technion.wiki.impl.tests;

import java.lang.reflect.Constructor;

import il.ac.technion.wiki.api.IWikiEngine;
import il.ac.technion.wiki.api.IWikiEngine.IWikiContent;
import il.ac.technion.wiki.api.search.ISearchExpression;
import il.ac.technion.wiki.api.IDataStore;

public final class DatastoreStub implements IDataStore {
	@Override
	public IWikiContent get(String name, int revision)
			throws NotFoundException, DataStoreException {
		
		last_action = Thread.currentThread().getStackTrace()[1].getMethodName();
		
		IWikiContent c;
		try {
			Constructor<?> con = wcClass.getConstructor(wikiEngine.getClass());
			c = (IWikiContent) con.newInstance(wikiEngine);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		
		c.setRevision(revision);
		c.setMetadata(IWikiContent.MetadataType.DATE, "15/5/2005");
		c.setMetadata(IWikiContent.MetadataType.AUTHOR, "Slava");
		c.setName("getted");
		c.setText("GettedText");
		c.apply();
		return c;
	}

	@Override
	public void put(IWikiContent content, Boolean insert)
			throws NotFoundException, NameExistsException, DataStoreException {
		last_action = Thread.currentThread().getStackTrace()[1].getMethodName();
		
	}

	@Override
	public void delete(String name) throws NotFoundException,
			DataStoreException {
		last_action = Thread.currentThread().getStackTrace()[1].getMethodName();
		
	}

	@Override
	public void rename(String name, String new_name) throws NotFoundException,
			NameExistsException, DataStoreException {
		last_action = Thread.currentThread().getStackTrace()[1].getMethodName();
		
	}

	@Override
	public IWikiContent[] search(ISearchExpression expr, int limit)
			throws DataStoreException {
		last_action = Thread.currentThread().getStackTrace()[1].getMethodName();
		return null;
	}

	@Override
	public void setContentClass(Class<? extends IWikiContent> clazz) {
		last_action = Thread.currentThread().getStackTrace()[1].getMethodName();
		wcClass = clazz;
	}
	
	public void setNeutralWords(String[] words) {
		// TODO Auto-generated method stub
	}

	@Override
	public void setWikiEngine(IWikiEngine wikiEngine) {
		last_action = Thread.currentThread().getStackTrace()[1].getMethodName();
		this.wikiEngine = wikiEngine;
	}
	
	public String last_action = null;
	private Class<? extends IWikiContent> wcClass;
	private IWikiEngine wikiEngine;
}
